home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / GameKit / gamekit-1 / SoundPlayer.m < prev    next >
Text File  |  1995-06-12  |  3KB  |  127 lines

  1.  
  2. #import <gamekit/gamekit.h>
  3. #import <daymisckit/daymisckit.h>
  4. #import <sound/sound.h>
  5.  
  6. #define GK_MIN_SOUND_TIME 50000 // in usec, this prevents flanging
  7.  
  8. @implementation SoundPlayer
  9.  
  10. - init
  11. { // This method should be avoided.
  12.     return [self initSounds:GK_DEFNUMSOUNDS types:GK_DEFNUMTYPES streams:1];
  13. }
  14.  
  15. - initSounds:(int)ns types:(int)nt streams:(int)nstr
  16. {
  17.     if (!_initialized) [super init];
  18.     else [self freeObjects];
  19.     numTypes = nt; numSounds = ns;
  20.     currentTime = [[DAYTime alloc] initWithCurrentTime];
  21.     startTime = [[DAYTime alloc] initWithCurrentTime];
  22.     streamGroup = [[GKSoundStream alloc] initStreams:nstr];
  23.     soundListList = [[List alloc] initCount:numTypes];
  24.     soundType = 0; _initialized = YES; turnedOn = YES;
  25.     return self;
  26. }
  27.  
  28. - appDidInit:sender    // forwarded by GameBrain
  29. {
  30.     id gameInfo = [[NXApp delegate] gameInfo];
  31.     [self initSounds:[gameInfo numSounds] types:[gameInfo numSoundTypes]
  32.             streams:[gameInfo numSoundStreams]];
  33.     [self loadSounds];
  34.     gameScreen = [[NXApp delegate] gameScreen];
  35.     preferences = [[NXApp delegate] preferencesBrain];
  36.     return self;
  37. }
  38.  
  39. - loadSounds
  40. {
  41.     int i, j; id tempSnd, tempList;
  42.     char *soundFile = (char *)malloc(MAXPATHLEN + 1);
  43.     id minTime = [[[DAYTime alloc] init] addMicroseconds:GK_MIN_SOUND_TIME];
  44.     id soundOut;
  45.     if (!(soundOut = [[NXSoundOut alloc] init]))
  46.         return self; // bail if no soundOut
  47.     [soundOut free]; //only needed it for the check.
  48.     // if we had 3.1 we could do more checks, I'd put it in, but then I'd
  49.     // be binary incompatible with 3.0!  :-(  It'll be here in the future*****
  50.     for (i=0; i<numTypes; i++) {
  51.         tempList = [[List alloc] initCount:numSounds];
  52.         for (j=0; j<numSounds; j++) {  // load in the the sounds
  53.             sprintf(soundFile, "%s/Sound%d.%d.snd",
  54.                     [NXApp appDirectory], j, i);
  55.             tempSnd = [[GKSound alloc] initFromSoundfile:soundFile];
  56.             if (!tempSnd)
  57.                 fprintf(stderr, "Unable to load sound \"%s\".\n", soundFile);
  58.             [tempSnd setPlayStream:streamGroup];
  59.             [tempSnd setTimeToPlay:minTime]; // ***** need a way to change this
  60.             [tempList addObject:tempSnd];
  61.         }
  62.         [soundListList addObject:tempList];
  63.     }
  64.     free(soundFile);
  65.     return self;
  66. }
  67.  
  68. - shutUpUntil:aTime
  69. {
  70.     if (startTime) [startTime free];
  71.     startTime = aTime;
  72.     return self;
  73. }
  74.  
  75. - turnOn:(BOOL)flag { turnedOn = flag; return self; }
  76.  
  77. - play:sender { return [self playNum:[sender tag]]; }
  78.  
  79. - playNum:(int)num
  80. {
  81.     // make sure we have the sound
  82.     if ((num < 0) || (num >= [[soundListList objectAt:0] count])) return nil;
  83.     [currentTime initWithCurrentTime];
  84.     // play the sound if the game's state allows it.
  85.     if ([currentTime isAfter:startTime] && turnedOn &&
  86.             ([gameScreen demoMode] ? [preferences demoSound] : YES)) {
  87.         // get pointer to appropriate sound data and then enqueue
  88.         // the sound data to be played
  89.         [[[soundListList objectAt:soundType] objectAt:num] play];
  90.     }
  91.     return self;
  92. }
  93.  
  94. - setSoundType:(int)num
  95. {
  96.     if (num > [soundListList count]) soundType = [soundListList count];
  97.     else if (num < 0) soundType = 0;
  98.     else soundType = num;
  99.     return self;
  100. }
  101.  
  102. - (int)soundType { return soundType; }
  103.  
  104. - soundNum:(int)num type:(int)type
  105. {
  106.     return [[soundListList objectAt:type] objectAt:num];
  107. }
  108.  
  109. - freeObjects
  110. {
  111.     [soundListList makeObjectsPerform:@selector(freeObjects)];
  112.     [soundListList freeObjects];
  113.     [soundListList free];
  114.     [streamGroup free];
  115.     [device free];
  116.     return self;
  117. }
  118.  
  119. - free
  120. {
  121.     [self freeObjects];
  122.     return nil;
  123. }
  124.  
  125.  
  126. @end
  127.